﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpAlgorithm
{
    class Battlefield
    {
        public Battlefield()
        {
            int testCount = Int32.Parse(TestConsole.ReadLine());
            
            for (int testCase = 1; testCase <= testCount; testCase++)
            {
                string[] input = TestConsole.ReadLine().Split(' ');
                int totalCount = Int32.Parse(input[0]);
                int[] count = new int[7];
                for (int i = 1; i < 7; i++)
                {
                    count[i] = Int32.Parse(input[i]);
                }
                int[,] board = new int[4, 4];
                for (int x = 1; x < 4; x++)
                {
                    input = TestConsole.ReadLine().Split(' ');
                    for (int y = 1; y < 4; y++)
                    {
                        board[x, y] = Int32.Parse(input[y - 1]);
                    }
                }
                List<int[]> pointList = new List<int[]>();
                pointList.Add(new int[4] { board[1, 1] + board[2, 2] + board[3, 3], 4, 5, 6 });
                pointList.Add(new int[4] { board[1, 1] + board[2, 3] + board[3, 2], 4, 6, 5 });
                pointList.Add(new int[4] { board[1, 2] + board[2, 1] + board[3, 3], 5, 4, 6 });
                pointList.Add(new int[4] { board[1, 2] + board[2, 3] + board[3, 1], 5, 6, 4 });
                pointList.Add(new int[4] { board[1, 3] + board[2, 1] + board[3, 2], 6, 4, 5 });
                pointList.Add(new int[4] { board[1, 3] + board[2, 2] + board[3, 1], 6, 5, 4 });
                pointList = pointList.OrderBy(s => s[0]).Reverse().ToList<int[]>();
                int restCount = totalCount;
                int sum = 0;
                while (restCount > 0)
                {
                    for (int i = 0; i < pointList.Count; i++)
                    {
                        int enermyA = pointList[i][1];
                        int enermyB = pointList[i][2];
                        int enermyC = pointList[i][3];
                        int aCount = Math.Min(count[1], count[enermyA]);
                        int bCount = Math.Min(count[2], count[enermyB]);
                        int cCount = Math.Min(count[3], count[enermyC]);
                        count[1] -= aCount;
                        count[enermyA] -= aCount;
                        count[2] -= bCount;
                        count[enermyB] -= bCount;
                        count[3] -= cCount;
                        count[enermyC] -= cCount;
                        restCount -= aCount;
                        restCount -= bCount;
                        restCount -= cCount;
                        sum += aCount * board[1, enermyA - 3];
                        sum += bCount * board[2, enermyB - 3];
                        sum += cCount * board[3, enermyC - 3];
                        Console.WriteLine(1 + " " + enermyA + " : " + aCount);
                        Console.WriteLine(2 + " " + enermyB + " : " + bCount);
                        Console.WriteLine(3 + " " + enermyC + " : " + cCount);
                    }
                }
                Console.WriteLine("Case #" + testCase + ": " + sum);
                Console.WriteLine();
            }
        }
    }
}
